package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type SellFeedbackStatus struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64                 `json:"id" bson:"id,omitempty"`
	Name        string                `json:"name" bson:"name"`             //名称
	Sort        int64                 `json:"sort" bson:"sort"`             //排序
	L           int64                 `json:"l" bson:"l"`                   //L
	R           int64                 `json:"r" bson:"r"`                   //R
	Level       int64                 `json:"level" bson:"level"`           //Level
	Link        string                `json:"link" bson:"link"`             //link
	Parent      *SellFeedbackStatus   `json:"parent" bson:"parent"`         //父级菜单
	ParentId    int64                 `json:"parentId" bson:"parentId"`     //父级菜单
	Children    []*SellFeedbackStatus `json:"children" bson:"children"`     //子菜单
	Color       string                `json:"color" bson:"color"`           //字体颜色
	Background  string                `json:"background" bson:"background"` //背景颜色
	Group       string                `json:"group" bson:"group"`           //分组
	Step        string                `json:"step" bson:"step"`             //阶段标识
	Warn        string                `json:"warn" bson:"warn"`             //警告标识
	CreateAt    time.Time             `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time             `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time            `json:"deleteAt" bson:"deleteAt"`
}

func (this *SellFeedbackStatus) DocName() string { return "SellFeedbackStatus" }
func (this *SellFeedbackStatus) GetId() int64    { return this.Id }
func (this *SellFeedbackStatus) SetId(id int64)  { this.Id = id }
func (this *SellFeedbackStatus) Create() error {
	return this.GetFactory().Create(this)
}
func (this *SellFeedbackStatus) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *SellFeedbackStatus) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *SellFeedbackStatus) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *SellFeedbackStatus) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *SellFeedbackStatus) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *SellFeedbackStatus) ToString() string {
	return string(this.ToBytes())
}
func (this *SellFeedbackStatus) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellFeedbackStatus) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellFeedbackStatus) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *SellFeedbackStatus) LoadParent() {
	if this.ParentId == 0 {
		return
	}
	this.Parent, _ = NewSellFeedbackStatusCrud().FindId(this.ParentId)
}
func (this *SellFeedbackStatus) LoadChildren() {
	children, _ := NewSellFeedbackStatusCrud().FindBy(ss.M{"parentId": this.Id}, []string{"id"}, 0, 0)
	this.Children = children
}
func (this *SellFeedbackStatus) ClearRelationsBeforeSave() mgo.Doc {
	this.Parent = nil
	this.Children = nil
	return this
}
func neverUsed_SellFeedbackStatus() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type SellFeedbackStatusAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *SellFeedbackStatus
	Session *ez.Session
}

func NewSellFeedbackStatusAccessControl(model *SellFeedbackStatus, action string, session *ez.Session) *SellFeedbackStatusAccessControl {
	ctrl := &SellFeedbackStatusAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.SellFeedbackStatusAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
